﻿Imports System.Data.OleDb
Public Class Form1
    Private navState As String
    Private UserName As String = ""
    Private Password As String = ""
    Private timeCountDown As Integer
    Private dbPath As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'UserName = "dozen.hsu@gmail.com"
        'Password = "Tellabs01"

        cmboxTimer.SelectedIndex = 0

        dbPath = ".\db1.mdb"
        timeCountDown = 0
        navState = "Start"
        cmboxDeta.SelectedIndex = 0

    End Sub

    Private Sub Wb_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles Wb.DocumentCompleted
        If navState = "Start" Then
            mnuLogin.Enabled = True
            navState = "ReadyForLogin"
            Exit Sub
        End If
        If navState = "login" Then
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=2"))
            navState = "ReadyForGet"
            MsgBox("登录成功！")
            StatusStrip1.Items(0).Text = "登录成功"
            ToolStripButton1.Enabled = True
            mnuLogin.Enabled = False
            btnTimer.Enabled = True
            Exit Sub
        End If
        ' If navState = "GetCat1" Then
        'StatusStrip1.Items(0).Text = "正在刷新街边小贩数据"
        'getData(1)
        'Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=2"))
        'navState = "GetCat2"
        'Exit Sub
        'End If
        If navState = "GetCat2" Then
            StatusStrip1.Items(0).Text = "正在刷新商业数据,页面1"
            getData(2)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=2&start=20"))
            navState = "GetCat22"
            Exit Sub
        End If
        If navState = "GetCat22" Then
            StatusStrip1.Items(0).Text = "正在刷新商业数据,页面2"
            getData(2)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=4"))
            navState = "GetCat4"
            Exit Sub
        End If
        If navState = "GetCat4" Then
            StatusStrip1.Items(0).Text = "正在刷新娱乐休闲数据"
            getData(4)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=6"))
            navState = "GetCat6"
            Exit Sub
        End If
        If navState = "GetCat6" Then
            StatusStrip1.Items(0).Text = "正在刷新交通数据"
            getData(6)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=7"))
            navState = "GetCat7"
            Exit Sub
        End If
        If navState = "GetCat7" Then
            StatusStrip1.Items(0).Text = "正在刷新地产置业数据"
            getData(7)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=8"))
            navState = "GetCat8"
            Exit Sub
        End If
        If navState = "GetCat8" Then
            StatusStrip1.Items(0).Text = "正在刷新奢侈品数据"
            getData(8)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=9"))
            navState = "GetCat9"
            Exit Sub
        End If
        If navState = "GetCat9" Then
            StatusStrip1.Items(0).Text = "正在刷新科技产品数据"
            getData(9)
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=10"))
            navState = "GetCat10"
            Exit Sub
        End If
        If navState = "GetCat10" Then
            StatusStrip1.Items(0).Text = "正在刷新军事武器数据"
            getData(10)
            navState = "ReadyForGet"
            StatusStrip1.Items(0).Text = "所有数据刷新完毕！"
            Exit Sub
        End If
    End Sub


    Private Sub mnuUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUser.Click
        UserName = InputBox("输入您的账号", , UserName)

    End Sub

    Private Sub mnuLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLogin.Click
        If Wb.ReadyState = 4 Then
            StatusStrip1.Items(0).Text = "正在登录……"
            Dim doc As HtmlDocument
            Dim a As String
            Dim table(3, 100) As String
            a = ""
            doc = Wb.Document
            For i = 0 To doc.GetElementsByTagName("input").Count - 1
                If doc.GetElementsByTagName("input").Item(i).GetAttribute("name") = "email" Then
                    doc.GetElementsByTagName("input").Item(i).SetAttribute("value", "dozen.hsu@gmail.com")
                End If
                If doc.GetElementsByTagName("input").Item(i).GetAttribute("name") = "password" Then
                    doc.GetElementsByTagName("input").Item(i).SetAttribute("value", "Tellabs01")
                End If
            Next
            doc.GetElementById("btn_dl").InvokeMember("click")
            navState = "login"
        End If
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        prolist.Items.Clear()
        If CheckBox1.Checked = True Then
            detailList.Items.Clear()
        End If
        navState = "GetCat2"
        Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=2"))



    End Sub

    Private Sub getData(ByVal ptype As Integer)
        Dim doc, d As HtmlDocument
        Dim a As String
        Dim temp As String
        Dim conn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim dr1 As OleDbDataReader
        Dim deta As Double
        Dim maxprice As Double, minprice As Double
        Try
            conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";")
            conn.Open()

            If conn.State.ToString = "Closed" Then
                Debug.Print("failed")
                Exit Sub
            End If
            a = ""
            doc = Wb.Document
            d = doc.GetElementById("r2_2a").Document

            For i = 0 To d.GetElementsByTagName("li").Count - 1
                If i Mod 5 = 1 Then
                    a = doc.GetElementsByTagName("li").Item(i).InnerText
                End If
                If i Mod 5 = 2 Then
                    temp = doc.GetElementsByTagName("li").Item(i).InnerText
                    Dim temp1 As String = ""

                    If temp.Trim.EndsWith("元") Then
                        temp = temp.Replace("元", "")
                    End If
                    If temp.Trim.EndsWith("万") Then
                        temp = temp.Replace("万", "") * 10000
                    End If
                    If temp.Trim.EndsWith("亿") Then
                        temp = temp.Replace("亿", "") * 100000000
                    End If
                    cmd = New OleDbCommand("SELECT * FROM dbProduct WHERE Pname='" & a & "'", conn)
                    dr1 = cmd.ExecuteReader
                    If dr1.Read Then
                        maxprice = dr1("MaxPrice")
                        minprice = dr1("MinPrice")
                        '判断价格
                        If temp > maxprice Then
                            cmd = New OleDbCommand("UPDATE dbProduct SET MaxPrice = " & maxprice & " WHERE Pname = '" & a & "'", conn)
                            cmd.ExecuteReader()
                            cmd.Dispose()
                            maxprice = temp
                            addInfo("物品<" & a & "> 产生了更高的价格" & maxprice & ",已更新", Color.WhiteSmoke)
                        End If
                        If temp < minprice Then
                            cmd = New OleDbCommand("UPDATE dbProduct SET MinPrice = " & minprice & " WHERE Pname = '" & a & "'", conn)
                            cmd.ExecuteReader()
                            cmd.Dispose()
                            minprice = temp
                            addInfo("物品<" & a & "> 产生了更低的价格" & minprice & ",已更新", Color.WhiteSmoke)
                        End If
                        Dim proListItem As ListViewItem
                        proListItem = prolist.Items.Add(a)
                        proListItem.SubItems.Add(minprice)
                        proListItem.SubItems.Add(maxprice)
                        proListItem.SubItems.Add(maxprice - minprice)
                        proListItem.SubItems.Add(temp)
                        proListItem.SubItems.Add(temp - minprice)
                        If temp = minprice Then
                            addInfo("物品<" & a & "> 处于最低价格" & minprice & ",建议买入", Color.FromArgb(100, 0, 255, 0))
                            proListItem.BackColor = Color.Green
                        End If
                        If temp = maxprice Then
                            addInfo("物品<" & a & "> 处于最高价格" & maxprice & ",建议卖出", Color.Red)
                            proListItem.BackColor = Color.Red
                        End If
                        deta = maxprice - minprice
                        If temp - minprice < deta * 0.1 Then
                            If Val(cmboxDeta.SelectedItem.ToString) >= 10 Then
                                addInfo("物品<" & a & "> 当前价格" & temp & "比最低价格高最大差价的10%内", Color.SpringGreen)
                            End If
                        ElseIf temp - minprice < deta * 0.2 Then

                            If Val(cmboxDeta.SelectedItem.ToString) >= 20 Then
                                addInfo("物品<" & a & "> 当前价格" & temp & "比最低价格高最大差价的20%内", Color.GreenYellow)
                            End If

                        ElseIf temp - minprice < deta * 0.3 Then

                            If Val(cmboxDeta.SelectedItem.ToString) >= 30 Then
                                addInfo("物品<" & a & "> 当前价格" & temp & "比最低价格高最大差价的30%内", Color.AliceBlue)
                            End If

                        ElseIf temp - minprice < deta * 0.4 Then
                            Debug.Print(Val(cmboxDeta.SelectedItem.ToString))
                            If Val(cmboxDeta.SelectedItem.ToString) >= 40 Then
                                addInfo("物品<" & a & "> 当前价格" & temp & "比最低价格高最大差价的40%", Color.SkyBlue)
                            End If
                        End If

                    Else
                        MsgBox("test ")
                        a = "'" & a & "'," & temp & "," & temp
                        Debug.Print(a)
                        cmd = New OleDbCommand(" INSERT INTO dbProduct (Pname, MaxPrice, MinPrice) VALUES (" & a & ")", conn)
                        cmd.ExecuteReader()
                        cmd.Dispose()
                        addInfo("添加了新的投资物品" & a & ",价格为" & temp, Color.White)
                    End If
                End If

            Next
            conn.Dispose()

        Catch

        End Try
    End Sub


    Private Sub mnuPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPassword.Click
        Password = InputBox("输入您的密码", , Password)

    End Sub

    Private Sub btnTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimer.Click
        If btnTimer.Text.IndexOf("开始") > 0 Then
            cmboxTimer.Enabled = False
            Timer1.Enabled = True
            btnTimer.Text = btnTimer.Text.Replace("开始", "结束")
        Else
            cmboxTimer.Enabled = True
            Timer1.Enabled = False
            timeCountDown = 0
            btnTimer.Text = btnTimer.Text.Replace("结束", "开始")
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (timeCountDown = Val(cmboxTimer.SelectedItem.ToString) * 60) Then
            timeCountDown = 0
            prolist.Items.Clear()
            If CheckBox1.Checked = True Then
                detailList.Items.Clear()
            End If
            navState = "GetCat2"
            Wb.Navigate(New Uri("http://www.kaixin001.com/!rich/market.php?cateid=2"))
            lblCountDown.Text = "刷新中……"
        Else
            timeCountDown = timeCountDown + 1
            lblCountDown.Text = "距离下次刷新还有" & (Val(cmboxTimer.SelectedItem.ToString) * 60 - timeCountDown) & "秒"

        End If
    End Sub

    Private Sub DatabasePathToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatabasePathToolStripMenuItem.Click
        Dim path As String
        path = InputBox("输入数据库路径", , dbPath)
        If path <> "" Then
            dbPath = path
        End If
    End Sub

    Private Sub ToolStripButton2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim d As HtmlDocument = Wb.Document
        Dim obj(1) As Object
        obj(0) = CObj(New String("108"))
        obj(1) = CObj(New String("1"))


        d.InvokeScript("purchase", obj)


    End Sub

    Public Sub addInfo(ByVal info As String, ByVal color As Color)
        Dim listitem As ListViewItem
        listitem = detailList.Items.Add(Now.ToString)
        listitem.SubItems.Add(info)
        listitem.BackColor = color

    End Sub
End Class

